/**
 * 会员管理--用户职业列表的单例对象
 */
var Occupation = {
    id: "OccupationTable",//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Occupation.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '职业', field: 'occupation', align: 'center', valign: 'middle', sortable: true},
        {title: '职业描述', field: 'desc', align: 'center', valign: 'middle', sortable: true},
        {title: '是否删除', field: 'isDelete', align: 'center', valign: 'middle', sortable: true},
        {title: '排序', field: 'sort', align: 'center', valign: 'middle', sortable: true},
        {title: '创建时间', field: 'createtime', align: 'center', valign: 'middle', sortable: true}]
    return columns;
};

/**
 * 检查是否选中
 */
Occupation.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Occupation.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加职业
 */
Occupation.openOccupation = function () {
    var index = layer.open({
        type: 2,
        title: '添加职业',
        area: ['800px', '500px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/occupation/occupation_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看职业详情
 */
Occupation.openOccupationDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '职业详情',
            area: ['800px', '500px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/occupation/occupation_update/' + Occupation.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除职业
 */
Occupation.delete = function () {
    if (this.check()) {
        if(Occupation.seItem.isDelete == "Y"){
            Feng.success("该记录已删除!");
        }else {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/occupation/delete", function (data) {
                    Feng.success("删除成功!");
                    Occupation.table.refresh();
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("occupationId", Occupation.seItem.id);
                ajax.start();
            };

            Feng.confirm("是否删除职业 " + Occupation.seItem.occupation + "?", operation);
        }
    }
};
/**
 * 取消删除职业
 */
Occupation.cancelDelete = function () {
    if (this.check()) {
        if(Occupation.seItem.isDelete == "N"){
            Feng.success("该记录未删除!");
        }else{
            var operation = function(){
                var ajax = new $ax(Feng.ctxPath + "/occupation/cancel_delete", function (data) {
                    Feng.success("取消删除成功!");
                    Occupation.table.refresh();
                }, function (data) {
                    Feng.error("取消删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("occupationId", Occupation.seItem.id);
                ajax.start();
            };
            Feng.confirm("是否取消删除职业 " + Occupation.seItem.occupation + "?", operation);
        }
    }
};
Occupation.search = function () {
    var queryData = {};
    queryData['occupation'] = $("#occupation").val();
    Occupation.table.refresh({query: queryData});
}

$(function () {
    var defaultColunms = Occupation.initColumn();
    var table = new BSTable(Occupation.id, "/occupation/list", defaultColunms);
    table.setPaginationType("server");
    Occupation.table = table.init();
});